import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.security.Key;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class demo01 {
    public static void main(String[] args) throws InterruptedException {
        //等待
        //test01();

        //tittle操作
        //test02();

        //浏览器操作
        //test03();

        //键盘事件
        //test04();

        //鼠标事件
        test05();
    }

    private static void test05() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩2表情包");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //找到图片按钮
        WebElement webElement=webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));

        //鼠标移动到目标后右键点击
        Actions actions=new Actions(webDriver);
        actions.moveToElement(webElement).contextClick().perform();
    }

    private static void test04() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("临渊行");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //control+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);

        //control+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(3000);

        //control+V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");

        webDriver.findElement(By.cssSelector("#kw")).sendKeys("漫画");
        webDriver.findElement(By.cssSelector("su")).click();
    }

    private static void test03() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩三表情包");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //后退
        webDriver.navigate().back();
        sleep(3000);

        //刷新
        webDriver.navigate().refresh();
        sleep(3000);

        //前进
        webDriver.navigate().forward();
        sleep(3000);

        //滚动条
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");

        //最大化
        webDriver.manage().window().maximize();
        sleep(3000);

        //全屏
        webDriver.manage().window().fullscreen();
        sleep(3000);

        //设置固定宽高
        webDriver.manage().window().setSize(new Dimension(500,500));
    }

    private static void test02() {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        String url=webDriver.getCurrentUrl();
        String title=webDriver.getTitle();
        if(url.equals("https://www.baidu.com/")&&title.equals("百度一下，你就知道")){
            System.out.println("当前页面url："+url+",当前页面title："+title);
            System.out.println("测试通过！");
        }else{
            System.out.println("测试不通过！！");
        }
    }

    private static void test01() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩坏3壁纸");
        webDriver.findElement(By.cssSelector("#su")).submit();

        //隐式等待，判断页面是否加载齐全,齐全才能继续运行，不能满足特定条件
        //隐式等待只需要声明一次，一般在打开浏览器后进行声明。声明之后对整个drvier的生命周期都有效，后面不用重复声明
        //webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);

        //显式等待，可以满足特定条件
        WebDriverWait wait = new WebDriverWait(webDriver,10);
        //判断元素是否可以被点击，只有满足条件才能继续运行
        //wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(7) > a")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));

        webDriver.findElement(By.cssSelector("#kw")).clear();
    }
}
